﻿/*------------------------------------------------------------------
Copyright (c) 2008 visualSingularity.net. All rights reserved.

This file is part of Hyperbolic control.

Hyperbolic control is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Hyperbolic control is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------*/
/*------------------------------------------------------------------
Copyright (c) 2008 visualSingularity.net. All rights reserved.

This file is part of Hyperbolic control.

Hyperbolic control is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Hyperbolic control is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hyperbolic.Model;
using Hyperbolic.Vectors;

namespace Hyperbolic
{
    public class TreeView : ItemsControl
    {
        ITreeModel _model;

        static TreeView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeView), new FrameworkPropertyMetadata(typeof(TreeView)));
        }

        public TreeView()
        {
            this.SetBinding(TreeView.ItemsSourceProperty, new Binding("NodeModelCollection"));
        }

        public void Attach(ITreeModel model)
        {
            _model = model;
            this.DataContext = _model;
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            //Do not allow to change the ItemsSource property
            if (oldValue == null)
                base.OnItemsSourceChanged(oldValue, newValue);
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            NodeModel __nodeModel = item as NodeModel;
            if (__nodeModel != null)
            {
                if (_model == null)
                    _model = __nodeModel.Model;
            }
        }

        protected override Size MeasureOverride(Size constraint)
        {
            if (_model != null)
            {
                _model.ContainerOrigin = new ScreenVector(constraint.Width / 2, constraint.Height / 2);
                _model.ContainerBounds = new ScreenVector(_model.ContainerOrigin);
            }
            return base.MeasureOverride(constraint);
        }

    }


}
